package com.xinlu.user.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.xinlu.common.game.bo.ConstantLstBo;
import com.xinlu.derek.common.BaseDao;
import com.xinlu.persistence.Users;
import com.xinlu.util.tool.DateUtil;
import com.xinlu.util.tool.PageObject;

public class UsersDao extends BaseDao {

	public Users getUserInfo(String username, String pwd) {

		String hql = " select U from Users U where U.username = ? and U.userpass = ? ";

		List<Users> list = super.find(hql, new Object[] { username, pwd });

		return (list != null && list.size() > 0) ? list.get(0) : null;
	}

	/**
	 * 
	 * Function : 修改用户信息
	 * 
	 * @param instance
	 *            Derek 2011-5-17 上午06:32:09
	 */
	public void updateUserInfo(Users instance) {

		super.update(instance);
	}

	/**
	 * 获取下级用户信息(包含自己)
	 * 
	 * @param crtUser
	 * @return
	 */
	public List<Users> loadXjyhs(Users crtUser) {
		String cmdText = "from Users where lock<>1 and regfrom like '%&" + crtUser.getUsername() + "&%' order by id desc";
		List<Users> lst = super.find(cmdText);
		if (lst == null) {
			lst = new ArrayList<Users>();
		}

		lst.add(crtUser); // 加上自己本身

		return lst;
	}
	
	/**
	 * 获取下一级用户信息(不包含自己)
	 * 
	 * @param crtUser
	 * @return
	 */
	public List<Users> loadXyjyhs(Users crtUser) {
		String cmdText = "from Users where lock<>1 and regfrom like '&" + crtUser.getUsername() + "&%'";
		List<Users> lst = super.find(cmdText);
		if (lst == null) {
			lst = new ArrayList<Users>();
		}

		return lst;
	}

	/**
	 * 获取团队余额
	 * 
	 * @param crtUser
	 * @return
	 */
	public Double getTdye(Users crtUser) {
		String cmdText = "select uermoney from Users where regfrom like '%&" + crtUser.getUsername() + "&%'";
		List lst = super.find(cmdText);
		if (lst == null) {
			lst = new ArrayList();
		}
		Double tdye = crtUser.getUermoney();
		for (int i = 0; i < lst.size(); i++) {
			tdye += ConstantLstBo.getDoubleValue(lst.get(i));
		}

		return tdye;
	}

	/**
	 * 更新用户
	 * 
	 * @param user
	 */
	public void update(Users user) {
		super.update(user);
	}

	/**
	 * 保存用户
	 * 
	 * @param user
	 */
	public void save(Users user) {
		super.save(user);
	}

	/**
	 * 加载会员列表
	 * 
	 * @param crtUser
	 * @param yhm
	 * @param yhz
	 * @param bgnYe
	 * @param endYe
	 * @param bgnAddTime
	 * @param endAddTime
	 * @param px
	 * @param cddx
	 * @return
	 */
	public PageObject loadMembers(String crtUserName, String yhm, String yhz, String bgnYe, String endYe, int pageNo) {

		String cmdText = "select * from Users where regfrom like '&" + crtUserName + "&%' ";
		if (StringUtils.isNotEmpty(yhm)) {
			cmdText += " and username like '" + yhm + "' ";
		}
		if ("代理".equals(yhz)) {
			cmdText += " and daili=4 ";
		} else if ("会员".equals(yhz)) {
			cmdText += " and daili=0 ";
		}
		if (ConstantLstBo.getDoubleValue(bgnYe) < ConstantLstBo.getDoubleValue(endYe)) {
			cmdText += " and uermoney >=" + bgnYe + " and uermoney <=" + endYe + " ";
		}

		return super.findPageObjectByProc(10, pageNo, cmdText, Users.class);
	}

	/**
	 * 加载用户
	 * 
	 * @param crtYhm
	 * @return
	 */
	public Users loadUser(String crtYhm) {
		
		List list = super.find("from Users where username='" + crtYhm + "'");
		return list != null && list.size() > 0 ? (Users) list.get(0) : null;
	}

	/**
	 * 判断用户是否存在
	 * 
	 * @param yhm
	 * @return
	 */
	public boolean existUser(String yhm) {

		String cmdText = "select username from Users where username='" + yhm + "'";

		List lst = super.find(cmdText);
		if (lst == null || lst.size() == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 今日注册
	 * 
	 * @param crtUser
	 * @return
	 */
	public List getJrzc(Users crtUser) {
		String bgnAddTime = DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, new Date()) + " 00:00:00";
		String endAddTime = DateUtil.getFormatDate(DateUtil.YYYY_MM_DD, new Date()) + " 23:59:59";
		String cmdText = "from Users where addtime>='" + bgnAddTime + "' and addtime<='" + endAddTime + "'" + " and regfrom like '%&" + crtUser.getUsername() + "&%'";
		return super.find(cmdText);
	}

	/**
	 * 
	 * Function : 根据用户名获取用户详细信息
	 * 
	 * @return Derek 2011-6-1 下午03:26:42
	 */
	public Users getUserinfo(String username) {

		String hql = " from Users u where u.username = ? ";

		List<Users> reList = super.find(hql, username);

		return reList != null && reList.size() > 0 ? reList.get(0) : null;
	}
}
